<?php

require_once ROOT_PATH . 'admin/models/ServiceAddressModel.php';
require_once ROOT_PATH . 'admin/models/ServiceAddressCatModel.php';

/* 初始化数据交换对象 */
$exc = new exchange($ecs->table("service_address_cat"), $db, 'cat_id', 'cat_name');

class ServiceAddressCatController {

  public function __construct() {
    $this->addressModel = new ServiceAddressModel();
  }

  public function showList() {
    $articlecat = $this->addressModel->article_cat_list(0, 0, false);
    foreach ($articlecat as $key => $cat) {
      $articlecat[$key]['type_name'] = $GLOBALS['_LANG']['type_name'][$cat['cat_type']];
    }
    $GLOBALS['smarty']->assign('ur_here', '归属地列表');
    $GLOBALS['smarty']->assign('action_link', array('text' => '添加归属地', 'href' => 'service_addresscat.php?act=add'));
    $GLOBALS['smarty']->assign('full_page', 1);
    $GLOBALS['smarty']->assign('articlecat', $articlecat);

    assign_query_info();
    $GLOBALS['smarty']->display('service/service_addresscat_list.htm');
  }

  public function add() {
    /* 权限判断 */
    admin_priv('article_cat');

    $GLOBALS['smarty']->assign('cat_select', $this->addressModel->article_cat_list(0));
    $GLOBALS['smarty']->assign('ur_here', '归属地添加');
    $GLOBALS['smarty']->assign('action_link', array('text' => '归属地列表', 'href' => 'service_addresscat.php?act=list'));
    $GLOBALS['smarty']->assign('form_action', 'insert');

    assign_query_info();
    $GLOBALS['smarty']->display('service/service_addresscat_info.htm');
  }

  public function insert() {
    /* 权限判断 */
    admin_priv('article_cat');

    /* 检查分类名是否重复 */
    $is_only = $GLOBALS['exc']->is_only('cat_name', $_POST['cat_name']);

    if (!$is_only) {
      sys_msg(sprintf($GLOBALS['_LANG']['catname_exist'], stripslashes($_POST['cat_name'])), 1);
    }


    $sql = "INSERT INTO " . $GLOBALS['ecs']->table('service_address_cat') . "(cat_name, parent_id, sort_order)
           VALUES ('$_POST[cat_name]', '$_POST[parent_id]', '$_POST[sort_order]')";
    $GLOBALS['db']->query($sql);


    admin_log($_POST['cat_name'], 'add', 'service_address_cat');

    $link[0]['text'] = '继续添加';
    $link[0]['href'] = 'service_addresscat.php?act=add';

    $link[1]['text'] = '返回列表';
    $link[1]['href'] = 'service_addresscat.php?act=list';
    clear_cache_files();
    sys_msg($_POST['cat_name'] . '成功添加', 0, $link);
  }

  public function edit() {
    /* 权限判断 */
    admin_priv('article_cat');

    $sql = "SELECT * FROM " .
            $GLOBALS['ecs']->table('service_address_cat') . " WHERE cat_id='$_REQUEST[id]'";
    $cat = $GLOBALS['db']->GetRow($sql);


    $options = $this->addressModel->article_cat_list(0, $cat['parent_id'], false);
    $select = '';
    $selected = $cat['parent_id'];
    foreach ($options as $var) {
      if ($var['cat_id'] == $_REQUEST['id']) {
        continue;
      }
      $select .= '<option value="' . $var['cat_id'] . '" ';
      $select .= ' cat_type="' . $var['cat_type'] . '" ';
      $select .= ($selected == $var['cat_id']) ? "selected='ture'" : '';
      $select .= '>';
      if ($var['level'] > 0) {
        $select .= str_repeat('&nbsp;', $var['level'] * 4);
      }
      $select .= htmlspecialchars($var['cat_name']) . '</option>';
    }
    unset($options);
    $GLOBALS['smarty']->assign('cat', $cat);
    $GLOBALS['smarty']->assign('cat_select', $select);
    $GLOBALS['smarty']->assign('ur_here', '编辑归属地');
    $GLOBALS['smarty']->assign('action_link', array('text' => '归属地列表', 'href' => 'service_addresscat.php?act=list'));
    $GLOBALS['smarty']->assign('form_action', 'update');

    assign_query_info();
    $GLOBALS['smarty']->display('service/service_addresscat_info.htm');
  }

  public function update() {
    /* 权限判断 */
    admin_priv('article_cat');

    /* 检查重名 */
    if ($_POST['cat_name'] != $_POST['old_catname']) {
      $is_only = $GLOBALS['exc']->is_only('cat_name', $_POST['cat_name'], $_POST['id']);

      if (!$is_only) {
        sys_msg(sprintf($GLOBALS['_LANG']['catname_exist'], stripslashes($_POST['cat_name'])), 1);
      }
    }

    if (!isset($_POST['parent_id'])) {
      $_POST['parent_id'] = 0;
    }



    /* 检查设定的分类的父分类是否合法 */
    $child_cat = $this->addressModel->article_cat_list($_POST['id'], 0, false);
    if (!empty($child_cat)) {
      foreach ($child_cat as $child_data) {
        $catid_array[] = $child_data['cat_id'];
      }
    }
    if (in_array($_POST['parent_id'], $catid_array)) {
      sys_msg(sprintf($GLOBALS['_LANG']['parent_id_err'], stripslashes($_POST['cat_name'])), 1);
    }



    $cat_name = $GLOBALS['db']->getOne("SELECT cat_name FROM " . $GLOBALS['ecs']->table('article_cat') . " WHERE cat_id = '" . $_POST['id'] . "'");
    if ($GLOBALS['exc']->edit("cat_name = '$_POST[cat_name]',parent_id = '$_POST[parent_id]',sort_order='$_POST[sort_order]'", $_POST['id'])) {
      if ($_POST['cat_name'] != $cat_name) {
        //如果分类名称发生了改变
        $sql = "UPDATE " . $GLOBALS['ecs']->table('nav') . " SET name = '" . $_POST['cat_name'] . "' WHERE ctype = 'a' AND cid = '" . $_POST['id'] . "' AND type = 'middle'";
        $GLOBALS['db']->query($sql);
      }

      $link[0]['text'] = '返回列表';
      $link[0]['href'] = 'service_addresscat.php?act=list';
      $note = sprintf('更新成功', $_POST['cat_name']);
      admin_log($_POST['cat_name'], 'edit', 'service_addresscat');
      clear_cache_files();
      sys_msg($note, 0, $link);
    } else {
      die($GLOBALS['db']->error());
    }
  }

  public function remove() {

    check_authz_json('article_cat');

    $id = intval($_GET['id']);



    $sql = "SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('service_address_cat') . " WHERE parent_id = '$id'";
    if ($GLOBALS['db']->getOne($sql) > 0) {
      /* 还有子分类，不能删除 */
      make_json_error('还有子分类，不能删除');
    }

    /* 非空的分类不允许删除 */
    $sql = "SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('service_address') . " WHERE cat_id = '$id'";
    if ($GLOBALS['db']->getOne($sql) > 0) {
      make_json_error(sprintf('非空的分类不允许删除'));
    } else {
      $GLOBALS['exc']->drop($id);
      clear_cache_files();
    }

    $url = 'service_addresscat.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);

    ecs_header("Location: $url\n");
    exit;
  }
  
  public function query() {
    $articlecat = $this->addressModel->article_cat_list(0, 0, false);
    foreach ($articlecat as $key => $cat)
    {
        $articlecat[$key]['type_name'] = $GLOBALS['_LANG']['type_name'][$cat['cat_type']];
    }
    $GLOBALS['smarty']->assign('articlecat', $articlecat);

    make_json_result($GLOBALS['smarty']->fetch('service/service_addresscat_list.htm'));    
  }

}